package com.example.myapplication;

import android.app.AppComponentFactory;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;


/**
 * Created by lyt on 2020/12/5 0005.
 */

public class RegisterActivity extends AppCompatActivity {
    private EditText username_RS;
    private EditText password_RS;
    private Button register;
    private MyDatabaseHelper dbHelper;
    private ImageView register_head;
    private static  final  String TAG="RegisterActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        getSupportActionBar().hide();

        username_RS = (EditText)findViewById(R.id.register_username_input);
        password_RS = (EditText)findViewById(R.id.register_password_input);
        register = (Button)findViewById(R.id.but_register);
        register_head = (ImageView)findViewById(R.id.register_head);
        dbHelper = new MyDatabaseHelper(this,"memo.db",null,1);
//        username_RS.setEnabled( true );
//        password_RS.setEnabled( true );
        //密码样式
        password_RS.setTransformationMethod(PasswordTransformationMethod.getInstance());


        register_head.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View v) {
                Intent intent = new Intent(RegisterActivity.this,LoginActivity.class);
                startActivity(intent);
            }
        });

        register.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                SQLiteDatabase db = dbHelper.getWritableDatabase();

                ContentValues values = new ContentValues();
                String username1 = username_RS.getText().toString().trim();
                String password1 = password_RS.getText().toString().trim();

                //检验用户名是否已存在


                if (CheckIsDataAlreadyInDBorNot(username1)) {
                    Toast.makeText(RegisterActivity.this, "该用户名已被注册，注册失败", Toast.LENGTH_SHORT).show();
                } else {

                    if (register(username1, password1)) {
                        Toast.makeText(RegisterActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
                        Intent intent = new Intent(RegisterActivity.this,LoginActivity.class);
                        startActivity(intent);
                    }

                    //插入数据
//                    values.put("username", username1);
//                    values.put("password", password1);
//                    db.insert("User", null, values);
//                    //Toast.makeText(RegisterActivity.this,"增加成功！！！",Toast.LENGTH_SHORT).show();
//                    db.close();
                }
            }
        });


    }

    //向数据库插入数据
    public boolean register(String username,String password){
        SQLiteDatabase db= dbHelper.getWritableDatabase();

        ContentValues values=new ContentValues();
        values.put("username",username);
        values.put("password",password);
        db.insert("User",null,values);
        db.close();
        //db.execSQL("insert into userData (name,password) values (?,?)",new String[]{username,password});
        return true;
    }
    public boolean CheckIsDataAlreadyInDBorNot(String value){

        SQLiteDatabase db=dbHelper.getWritableDatabase();
        String Query = "Select * from User where username =?";
        Cursor cursor = db.rawQuery(Query,new String[] { value });
        if (cursor.getCount()>0){
            cursor.close();
            return  true;
        }
        cursor.close();
        return false;
    }

}
